package com.better.common.jwtauth.secruity;

import com.better.backend.mqReceiverService.UserInfo.model.UserModel;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

public final class JwtUserFactory {

    private JwtUserFactory() {
    }

    public static JWTUserDetails create(UserModel user) {
        return new JWTUserDetails(
                user.getId(),
                user.getLoginName(),
                user.getPassword(),
                user.getEmail(),
                user.getName(),
                user.getPhone(),
                user.getMobile(),
                user.getSchoolType(),
                user.getSchoolId(),
                user.getSchoolName(),
                user.getClassId(),
                user.getClassName(),
                user.getGradeId(),
                user.getGradeName(),
                mapToGrantedAuthorities(user.getRoles()),
                user.getLastPasswordResetDate(),
                user.getStudyId(),
                user.getStudyName(),
                user.getUserType()
        );
    }

    private static List<GrantedAuthority> mapToGrantedAuthorities(List<String> authorities) {
        if (CollectionUtils.isEmpty(authorities)) {
            return null;
        }
        return authorities.stream()
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());
    }
}

